angular.module('todointech',['ionic','todo.service'])
        .controller('TodoIntechCtrl', function($scope, $ionicModal, $ionicPopup, SQLService){
            
            SQLService.setup();
    
            // create function loadTask
            $scope.loadTask = function(){
//                // Create variable array tasks
//                $scope.tasks = [
//                    {'task_id':1, 'task_name':'Task 1'},
//                    {'task_id':2, 'task_name':'Task 2'},
//                    {'task_id':3, 'task_name':'Task 3'},
//                ];
                SQLService.all().then(function(results){
                    $scope.tasks = results;
                });
            }
            // Call loadTask function
            $scope.loadTask();
            
            // Create modal
            $ionicModal.fromTemplateUrl('new-task.html', function(modal){
                // Create variable opject from modal
                $scope.taskModal = modal;
            },{
                scope:$scope,
                animation:'slide-in-up'
            });
            
            // Create function to show form create task
            $scope.newTask = function(){
                $scope.taskModal.show();
            }
            // Create function to close task modal
            $scope.closeNewTask = function(){
                $scope.taskModal.hide();
            }
            
            // Create function to create new task
            $scope.createTask = function(task){
                SQLService.set(task.title);
                $scope.loadTask();
                $scope.taskModal.hide();
                task.title = ''
            }
            //Create function to Delete task
            $scope.onItemDelete = function(taskid){
                $ionicPopup.confirm({
                    title:'Confirm delete',
                    content:'Are you sure you want to delete this task'
                }).then(function(res){
                    if(res){
                        // Delete task
                        SQLService.delete(taskid);
                        $scope.loadTask();
                    }
                });
            }
            
            // Create function to edit task
            $scope.onItemEdit = function(taskid){
                $ionicPopup.prompt({
                    title:'Update task',
                    subTitle:'Enter task to update'
                }).then(function(res){
                    if(res){
                        // Edit task
                        SQLService.edit(res, taskid);
                        $scope.loadTask();
                    }
                });
            }
            
            // Create function to order task
            $scope.moveItem = function(item, fromIndex, toIndex) {
                //Move the item in the array
                console.log(item+':'+fromIndex+":"+toIndex);
                $scope.tasks.splice(fromIndex, 1);
                $scope.tasks.splice(toIndex, 0, item);
            }
            
        });